package com.gdkm.util;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class StringUtils {

	// 创建日期格式
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/** 将包含HTML代码的字符串去除html标签 */
    public static String stripHtml(String content) { 
        //<p>段落替换为换行 
        content = content.replaceAll("<p .*?>", "\r\n"); 
        //<br><br/>替换为换行 
        content = content.replaceAll("<br\\s*/?>", "\r\n"); 
        //去掉其它的<>之间的东西 
        content = content.replaceAll("\\<.*?>", "");
        //去掉&nbsp;
        content = content.replaceAll("&nbsp;", ""); 
        //去掉空白
        content = content.replaceAll(" ", ""); 
        return content;   
    }    
    
	/** 截取指定字节数的字符串,且确保汉字不被拆分 */
	public static String cutString(String text, int textMaxChar) {
		int size, index;
		String result = null;
		if (textMaxChar <= 0) {
			result = text;
		} else {
			for (size = 0, index = 0; index < text.length()
					&& size < textMaxChar; index++) {
				size += text.substring(index, index + 1).getBytes().length;
			}
			result = text.substring(0, index);
		}
		return result;
	}

	/** 按yyyy-MM-dd格式格式化日期 */
	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		} else {
			return df.format(date);
		}
	}
	
	/** 按yyyy-MM-dd HH:mm格式格式化日期 */
	public static String formatDateTime(Date date) {
		if (date == null) {
			return "";
		} else {
			return df2.format(date);
		}
	}

}
